package com.fourool.goodsmall.controller;

import com.fourool.goodsmall.common.Result;
import com.fourool.goodsmall.dto.LoginDTO;
import com.fourool.goodsmall.dto.RegisterDTO;
import com.fourool.goodsmall.service.GPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fourool.goodsmall.dto.UpdateInfoDTO;
import com.fourool.goodsmall.entity.GPerson;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private GPersonService personService;

    // 用户注册
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        return personService.register(registerDTO);
    }

    // 用户登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        return personService.login(loginDTO.getPhone(), loginDTO.getPassword());
    }

    // 获取会员信息
    @GetMapping("/info/{pid}")
    public Result getInfo(@PathVariable Integer pid) {
        return personService.getUserInfo(pid);
    }

    // 修改个人资料
    @PutMapping("/update/{pid}")
    public Result updateInfo(@PathVariable Integer pid, @RequestBody UpdateInfoDTO updateInfoDTO) {
        // 直接将pid和dto传递给service层处理
        return personService.updateUserInfo(pid, updateInfoDTO);
    }
}